package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.ImgCode;
import com.mofang.powerdekorhelper.model.User;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.LoginView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/17 0017.
 */

public class LoginPersenter extends BasePresent<LoginView> {

    Call<User> userCall;
    Call<ImgCode> imgCodeCall;
    RetrofitSerives retrofitSerives;

    public void getLoginResult(String userName, String passWord, String organizedCode, String departmentCode, final String validate, String vkey) {

        view.showProgress();

        JSONObject object = new JSONObject();
        try {
            object.put("user_name", userName);
            object.put("user_pwd", passWord);
            object.put("zzh", organizedCode);
            object.put("bmh", departmentCode);
            if (!validate.equals("")) {
                object.put("vKey", vkey);
            }

            if (!vkey.equals("")) {
                object.put("mcode", validate);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        String jsonParams = object.toString();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        userCall = retrofitSerives.getLoginResult(body);

        userCall.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if (response.isSuccessful()) {
                    view.setLoginResult(response.body());
                } else {
                    view.onError(Constants.LOGIN_ERROR_INFO);
                }

                view.hideProgress();
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
                view.hideProgress();
            }
        });
    }

    //
    public void getValidate() {

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.SellerAddress);
        imgCodeCall = retrofitSerives.getImgCode();
        imgCodeCall.enqueue(new Callback<ImgCode>() {
            @Override
            public void onResponse(Call<ImgCode> call, Response<ImgCode> response) {
                if (response.isSuccessful()) {
                    view.setImageCode(response.body());
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ImgCode> call, Throwable t) {
                view.onError("服务器请求异常，稍候再试" + t.getMessage());
            }
        });
    }

    @Override
    public void stopRequest() {

    }
}
